/**
 * 
 */
package com.chocolatefordogs.writersnb.app.project;

import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.MutableTreeNode;
import javax.swing.tree.TreePath;

import com.chocolatefordogs.writersnb.app.treeutils.TreeNodeBuilder;
import com.chocolatefordogs.writersnb.projects.Project;

/**
 * @author Matthew
 * 
 */
public final class ProjectTree {

	public ProjectTree() {
		rootNode = new DefaultMutableTreeNode("Root Node");
		treeModel = new DefaultTreeModel(rootNode);
		tree = new JTree(treeModel);
	}

	public JTree getTree() {
		return tree;
	}

	public void addProject(final Project prj) {
		treeModel.insertNodeInto(buildProjectNode(prj), (DefaultMutableTreeNode) treeModel.getRoot(), treeModel.getChildCount(rootNode));
		tree.expandPath(new TreePath(rootNode.getPath()));
	}

	private MutableTreeNode buildProjectNode(final Project prj) {
		final MutableTreeNode res = prjNodeBldr.buildNode(prj);
		return res;
	}

	private final JTree tree;
	private final DefaultMutableTreeNode rootNode;
	private final DefaultTreeModel treeModel;
	private final TreeNodeBuilder<Project> prjNodeBldr = ProjectNodeBuilder.getInstance();
}
